package com.gzz.orchid.common.boot.mybatis.interceptor;

import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.session.ResultHandler;

import java.sql.Statement;
import java.util.Properties;

/**
 * @author higmd
 */
@Slf4j
@Intercepts({
        @Signature(type = StatementHandler.class, method = "query", args = {Statement.class, ResultHandler.class}),
        @Signature(type = StatementHandler.class, method = "update", args = {Statement.class})
})
public class PerformanceInterceptor implements Interceptor {
    private long maxTolerate;
    @Override
    public Object intercept(Invocation inv) throws Throwable {
        log.debug("进入了注解。PerformanceInterceptor。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。");
        long startTime = System.currentTimeMillis();
        Object retVal = inv.proceed();
        long endTime = System.currentTimeMillis();
        log.debug("方法执行耗时：{}ms", endTime - startTime);
        return retVal;
    }
    @Override
    public Object plugin(Object target) {
        log.info("日志Plugin: {}", target.getClass().getName());
        return Plugin.wrap(target, this);
    }
    @Override
    public void setProperties(Properties properties) {
        log.info("日志Properties: {}", properties);
        this.maxTolerate = Long.parseLong(properties.getProperty("maxTolerate"));
    }

}
